package com.goldman.oss.common.internal.processor.context;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;


@Slf4j
public class OssCommonApplicationContextHolder implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    public OssCommonApplicationContextHolder() {
    }

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        if (applicationContext != null) {
            throw new IllegalStateException("OssCommonApplicationContextHolder already holded 'applicationContext'.");
        } else {
            applicationContext = context;
            log.info("holded applicationContext,显示名称:" + applicationContext.getDisplayName());
        }
    }

    public static ApplicationContext getApplicationContext() {
        if (applicationContext == null) {
            throw new IllegalStateException("'applicationContext' property is null,OssCommonApplicationContextHolder not yet init.");
        } else {
            return applicationContext;
        }
    }
}
